<?php


namespace app\websocket\model;


use think\Model;

class Order extends Model
{

    /**
     * getUserPaidOrderPaidCost
     * 获取用户今天的回款金额
     * @url
     * @method
     * @param $user
     * @return int
     * @author Asira
     * @date 2021/2/1
     */
    public static function getUserPaidOrderPaidCost($user): int
    {
        $OrderClass=new self();

        $result=ActionRole::checkUserActionRole($user,'order');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['oi.create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['oi.create_group']=['=',$group];
        }

        $where['o.del_flag']=['=',0];
        $where['oi.del_flag']=['=',0];
        $where['oi.gift']=['=',0];
        $date=date('Y-m-d');
        $where['o.payment_time']=['between',[$date.' 00:00:00',$date.' 23:59:59']];

        $obj=$OrderClass->alias('o')
            ->join('order_item oi','o.id=oi.order_id','left')
            ->where($where)
            ->where('o.pay_type','in',[4,6,7])
            ->field('sum(o.amount_paid*(oi.selling_price/o.total_price)) price')->find();
        $data=$obj->toArray();
        if(empty($data)){
            $cost=0;
        }else{
            $cost=round($data['price'],2);
        }

        return $cost;
    }


    /**
     * getOrderHistogramData
     * 获得最近的订单销售情况
     * @url
     * @method
     * @param $user
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author Asira
     * @date 2021/2/2
     */
    public static function getOrderHistogramData($user){
        $OrderClass=new self();
        $result=ActionRole::checkUserActionRole($user,'order');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['create_group']=['=',$group];
        }
        $days=6;
        for($i=$days;$i>0;$i--){
            $day_label[]=date('m-d',strtotime("-$i day"));
        }
        $day_label[]= date('m-d');
        $date_start=date('Y-m-d',strtotime("-$days day"));
        $date_end=date('Y-m-d');

        $where['create_time']=['between',[$date_start.' 00:00:00',$date_end.' 23:59:59']];
//        $where['create_user'] = ['=', $user];
        $where['del_flag']=['=',0];
        $where['order_status']=['=',1];
        $data=$OrderClass
            ->where($where)
            ->field('sum(total_price) total_price,sum(amount_discount) amount_discount,sum(amount_paid) amount_paid,sum(amount_refund) amount_refund,sum(tip_paid) tip_paid,date_format(create_time,\'%m-%d\') days')
            ->group('days')
            ->select()->toArray();
        $histogram_data=['type'=>'bar','title'=>['text'=>'近 '.($days+1).' 天账单情况'],'xRorate'=>0,'labels'=>$day_label];
        $total_price=['label' => "总额",'data'=>[]];
        $amount_paid=['label' => "已付",'data'=>[]];
        $amount_refund=['label' => "退款",'data'=>[]];
        $tip_paid=['label' => "小费",'data'=>[]];
        if (empty($data)) {
            $total_price['data'] =[0, 0, 0, 0, 0];
            $amount_paid['data'] = [0, 0, 0, 0, 0];
            $amount_refund['data'] = [0, 0, 0, 0, 0];
            $tip_paid['data'] =[0, 0, 0, 0, 0];
        }else{
            $order_data=[];
            foreach ($data as $key=>$value){
                $value['total_price']=round($value['total_price']-$value['amount_discount'],2);

                $value['total_price']=round($value['total_price'],2);
                $value['amount_paid']=round($value['amount_paid'],2);
                $value['amount_refund']=round($value['amount_refund'],2);
                $value['tip_paid']=round($value['tip_paid'],2);

                $order_data[$value['days']]=$value;
            }

            foreach ($day_label as $key=>$value){
                $total_price['data'][]=$order_data[$value]['total_price']??0;
                $amount_paid['data'][]=$order_data[$value]['amount_paid']??0;
                $amount_refund['data'][]=$order_data[$value]['amount_refund']??0;
                $tip_paid['data'][]=$order_data[$value]['tip_paid']??0;
            }
        }
        $histogram_data['datasets']=[$total_price,$amount_paid,$amount_refund,$tip_paid];
        return $histogram_data;
    }

    public static function getOrderUnpaidNum($user){
        $OrderClass=new self();
        $result=ActionRole::checkUserActionRole($user,'order');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['create_group']=['=',$group];
        }

        $where['pay_type']=['in',[2,3]];

        $data=$OrderClass->where($where)->field('
        ifnull(count(id),0) num,
        ifnull(count(if(pay_type=2,true,null)),0) unpaid_num,
        ifnull(count(if(pay_type=3,true,null)),0) partial_paid_num')->find();
        return $data;
    }

    public static function getOrderUnShipNum($user){
        $OrderClass=new self();
        $result=ActionRole::checkUserActionRole($user,'order');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['create_group']=['=',$group];
        }

        $where['logistics_status']=['in',[0,1]];
        $where['pay_type']=['=',4];

        $data=$OrderClass->where($where)->field('
        ifnull(count(id),0) num,
        ifnull(count(if(logistics_status=0,true,null)),0) no_stock_num,
        ifnull(count(if(logistics_status=1 and waybill_id is null,true,null)),0) wait_stock_num,
        ifnull(count(if(logistics_status=1 and waybill_id is not null and stock_status=1,true,null)),0) unstock_num,
        ifnull(count(if(stock_status=2,true,null)),0) stock_num
        ')->find();
        return $data;
    }
}